/*
 * Copyright (c) 2013-2015 Charkey. All rights reserved.
 *
 * This software is the confidential and proprietary information of Charkey.
 * You shall not disclose such Confidential Information and shall use it only
 * in accordance with the terms of the agreements you entered into with Charkey.
 *
 * Charkey MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE SOFTWARE,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
 *
 * Charkey SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,
 * MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 */

package cn.simastudio.talos.core.service.base.impl;

import cn.simastudio.talos.core.mapper.base.AddressCodeMapper;
import cn.simastudio.talos.core.model.base.AddressCode;
import cn.simastudio.talos.core.service.base.AddressCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;
import java.util.Objects;

/**
 * Usage: 省市县行政区划代码Service实现类
 *
 * @author Charkey
 * @date 2015/6/29 16:37
 */
@SuppressWarnings("SpringJavaAutowiringInspection")
@Service
public class AddressCodeServiceImpl implements AddressCodeService {

    @Autowired
    private AddressCodeMapper addressCodeMapper;

    @Override
    public String getAddressByCode(Integer addressCode) {
        Integer provinceCode = addressCode / 10000 * 10000;
        Integer cityCode = addressCode / 100 * 100;
        LinkedList<Integer> codeList = new LinkedList<>();
        codeList.add(provinceCode);
        if (!Objects.equals(cityCode, provinceCode)) {
            codeList.add(cityCode);
        }
        if (!Objects.equals(addressCode, cityCode)) {
            codeList.add(addressCode);
        }
        List<AddressCode> addressCodeList = addressCodeMapper.getAddressByCodes(codeList);
        String address = "";
        for (AddressCode code : addressCodeList) {
            address += code.getName();
        }
        return address;
    }
}
